package erp

import (
	"context"
	"encoding/json"
	"ez/apps/erp/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type SellOrderPayment struct {
	mgo.BaseDoc `bson:"-" json:"-"`
	Id          int64             `json:"id" bson:"id,omitempty"`
	BelongTo    *SellOrder        `json:"belongTo" bson:"belongTo"`     //所属
	BelongToId  int64             `json:"belongToId" bson:"belongToId"` //所属
	Type        *SellOrderPayType `json:"type" bson:"type"`             //付款类型
	TypeId      int64             `json:"typeId" bson:"typeId"`         //付款类型
	Money       float64           `json:"money" bson:"money"`           //支付金额
	ApplyEmp    *Emp              `json:"applyEmp" bson:"applyEmp"`     //申请人
	ApplyEmpId  int64             `json:"applyEmpId" bson:"applyEmpId"` //申请人
	PayAt       time.Time         `json:"payAt" bson:"payAt"`           //支付时间
	PayFeed     ss.M              `json:"payFeed" bson:"payFeed"`       //支付凭据
	PayEmp      *Emp              `json:"payEmp" bson:"payEmp"`         //付款人
	PayEmpId    int64             `json:"payEmpId" bson:"payEmpId"`     //付款人
	IsPay       bool              `json:"isPay" bson:"isPay"`           //是否付款
	IsLock      bool              `json:"isLock" bson:"isLock"`         //是否锁定
	CreateAt    time.Time         `json:"createAt" bson:"createAt"`
	UpdateAt    time.Time         `json:"updateAt" bson:"updateAt"`
	DeleteAt    *time.Time        `json:"deleteAt" bson:"deleteAt"`
}

func (this *SellOrderPayment) DocName() string { return "SellOrderPayment" }
func (this *SellOrderPayment) GetId() int64    { return this.Id }
func (this *SellOrderPayment) SetId(id int64)  { this.Id = id }
func (this *SellOrderPayment) Create() error {
	return this.GetFactory().Create(this)
}
func (this *SellOrderPayment) Replace() error {
	return this.GetFactory().Replace(this)
}
func (this *SellOrderPayment) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *SellOrderPayment) Delete() error {
	return this.GetFactory().Delete(this)
}
func (this *SellOrderPayment) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *SellOrderPayment) Destroy() error {
	return this.GetFactory().Destroy(this)
}
func (this *SellOrderPayment) ToString() string {
	return string(this.ToBytes())
}
func (this *SellOrderPayment) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *SellOrderPayment) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *SellOrderPayment) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
func (this *SellOrderPayment) LoadBelongTo() {
	if this.BelongToId == 0 {
		return
	}
	this.BelongTo, _ = NewSellOrderCrud().FindId(this.BelongToId)
}
func (this *SellOrderPayment) LoadType() {
	if this.TypeId == 0 {
		return
	}
	this.Type, _ = NewSellOrderPayTypeCrud().FindId(this.TypeId)
}
func (this *SellOrderPayment) LoadApplyEmp() {
	if this.ApplyEmpId == 0 {
		return
	}
	this.ApplyEmp, _ = NewEmpCrud().FindId(this.ApplyEmpId)
}
func (this *SellOrderPayment) LoadPayEmp() {
	if this.PayEmpId == 0 {
		return
	}
	this.PayEmp, _ = NewEmpCrud().FindId(this.PayEmpId)
}
func (this *SellOrderPayment) ClearRelationsBeforeSave() mgo.Doc {
	this.BelongTo = nil
	this.Type = nil
	this.ApplyEmp = nil
	this.PayEmp = nil
	return this
}
func neverUsed_SellOrderPayment() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type SellOrderPaymentAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *SellOrderPayment
	Session *ez.Session
}

func NewSellOrderPaymentAccessControl(model *SellOrderPayment, action string, session *ez.Session) *SellOrderPaymentAccessControl {
	ctrl := &SellOrderPaymentAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.SellOrderPaymentAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
